import flash.display.Sprite;
import flash.display.Bitmap;
import flash.display.BitmapData;
import flash.events.Event;
import flash.geom.Point;
import flash.geom.Rectangle;
import flash.geom.Matrix;
import flash.utils.Timer;

import haxe.FastList;
import haxe.Timer;

import nme.Assets;
import TileModel;
import HealthBarView;
import BaseLevel;
import Kongregate;

import flash.text.TextField;
import flash.text.TextFieldAutoSize;
import flash.text.TextFormat;
import flash.text.TextFormatAlign;

class CircleSquareLevel extends BaseLevel
{
  
  private var _pinkMap:Array<Array<Int>>;  
  private static inline var MAX_TILE_SIZE:Int = 64;
  private static inline var MAP_COLUMNS:Int = 19;
  private static inline var MAP_ROWS:Int = 8;
  
  //tile ID s in tile sheet
  private static inline var BOX:Int = 00;
  private static inline var SKY:Int = 10;
  private static inline var PLATFORM:Int = 11;
  private static inline var MACHINE_GUN:Int = 01;
  private static inline var PISTOL:Int = 02;
  private static inline var SHOTGUN:Int = 03;
  private static inline var DOOR:Int = 13;

  private static inline var PINK:Int = 21;
  private static inline var BLUE:Int = 20;

  private static inline var PinkRadius:Float = 20.5;
  private static inline var BlueRadius:Float = 20.5;
  public var _CircleModel:CircleModel;
  public var _CircleView:PlayerCircleView;
  public var _CircleController:CircleController;
  public var _s1:SquareModel;
  public var _s1_View:SquareView;
  public var _s2:SquareModel;
  public var _s2_View:SquareView;
  public var _s3:SquareModel;
  public var _s3_View:SquareView;
  public var _VectorModel:VectorModel;
  public var _VectorView:VectorView;
  public var _VectorTextView:VectorTextView;


  public var _cornerVectorModel:VectorModel;
  public var _cornerVectorView:VectorView;
  public var _cornerVectorTextView:VectorTextView;


  private var _SpriteLayer:Sprite;
  public var  WelcomeText:TextField;
  public var  CircleSquareExplationText1:TextField;
  public var RegionText:TextField;
  public var  ForumlaText1:TextField;
  public var  ForumlaTitle1:TextField;
  public var  CircleSquareExplationText2:TextField;
  public var  ForumlaText2:TextField;
  public var  ForumlaTitle2:TextField;
  public var  CircleSquareExplationText3:TextField;
  public var  ForumlaText3:TextField;
  public var  ForumlaText4:TextField;
  public var  ForumlaTitle4:TextField;

  public var  CircleSizeText:TextField;

  public function new(stage:Dynamic){
  super(stage);
  MAP_COLUMNS = 40;
    removeChild(_staticBackgroundBitmap);
    removeChild(_backgroundBitmap);
    removeChild(_foregroundBitmap);
  _staticBackgroundBitmapData = Assets.getBitmapData("Cardboard_Background.png");
  _staticBackgroundBitmap = new Bitmap(_staticBackgroundBitmapData);

  _backgroundBitmapData = new BitmapData(MAP_COLUMNS * MAX_TILE_SIZE, 
          MAP_ROWS * MAX_TILE_SIZE, true, 0);
  _backgroundBitmap = new Bitmap(_backgroundBitmapData);

  _foregroundBitmapData = new BitmapData(MAP_COLUMNS * MAX_TILE_SIZE, 
          MAP_ROWS * MAX_TILE_SIZE, true, 0);
  _foregroundBitmap = new Bitmap(_foregroundBitmapData);

  _SpriteLayer = new Sprite();
  _SpriteLayer.graphics.beginFill(0x0000000,0);
  _SpriteLayer.graphics.drawRect(0,0,MAP_COLUMNS * MAX_TILE_SIZE,MAP_ROWS * MAX_TILE_SIZE);
  _SpriteLayer.visible = true;

  _camera = new Rectangle (0, 0, stage.stageWidth, stage.stageHeight);

    addChild(_staticBackgroundBitmap);
    addChild(_backgroundBitmap);
    addChild(_foregroundBitmap);
    addChild(_SpriteLayer);
  _platformMap = 
	[
		  [PLATFORM,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,PLATFORM],
		  [PLATFORM,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,PLATFORM],
		  [PLATFORM,SKY,SKY,SKY,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1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1],
	  ]  ;


  buildReserveEnemies();


  // myTimer = new Timer(12);
  //   myTimer.addEventListener("timer", OnEnter);
  //   myTimer.start();
  //Add the game bitmaps
			  
			  //Run the buildMap method to convert the
			  //map array data into a visual display
			  

    RegionText  = new TextField();
    RegionText.htmlText = "<font size ='16'></font>";
    RegionText.x = 360;RegionText.y = 5;
    RegionText.width = 200; RegionText.height = 25;
    RegionText.wordWrap = true;
    RegionText.border = true;
    _SpriteLayer.addChild(RegionText);

    CircleSquareExplationText1 = new TextField();
    CircleSquareExplationText1.htmlText = "<font size ='16'>To determine whether a circle and square  have collided, you first have to determine on where the position of the circle in relation to the square. Which is as simple as comparing the circle's center in relation to the square's sides.</font>";
    CircleSquareExplationText1.x = 150;CircleSquareExplationText1.y = 5;
    CircleSquareExplationText1.width = 200; CircleSquareExplationText1.height = 170;
    CircleSquareExplationText1.wordWrap = true;
    CircleSquareExplationText1.border = true;
    _SpriteLayer.addChild(CircleSquareExplationText1);

    ForumlaText1 = new TextField();
    ForumlaText1.width = 340; ForumlaText1.height = 410;
    ForumlaText1.htmlText = "<font size ='16'>var region:String = \"\";
    if(_c1.yPos &lt; _s1.yPos)
    {
      if(_c1.xPos &lt; _s1.xPos)
      {	region = \"topLeft\";  }
      else if (_c1.xPos &gt; _s1.xPos + _s1.width)
      {	region = \"topRight\";    }
      else
      {	region = \"topMiddle\";  }
    }
    else if (_c1.yPos &gt; _s1.yPos + _s1.height)
    {
      if(_c1.xPos &lt; _s1.xPos)
      {	region = \"bottomLeft\";     }
      else if (_c1.xPos &gt; _s1.xPos + _s1.width )
      {	region = \"bottomRight\";      }
      else
      {	region = \"bottomMiddle\";    }
    }
    else
    {
      if(_c1.xPos &lt; _s1.xPos )
      {	region = \"leftMiddle\";    }
      else
      {	region = \"rightMiddle\";    }
    }</font>";
    ForumlaText1.x = 570;ForumlaText1.y = 20;

    ForumlaText1.wordWrap = true;
    ForumlaText1.border = true;
    _SpriteLayer.addChild(ForumlaText1);

    ForumlaTitle1 = new TextField();
    ForumlaTitle1.width = 240; ForumlaTitle1.height = 25;
    ForumlaTitle1.htmlText = "<font size ='16'>Click and drag to view full text</font>";
    ForumlaTitle1.x = 650;ForumlaTitle1.y = 0;

    ForumlaTitle1.wordWrap = true;
    //ForumlaTitle1.border = true;
    _SpriteLayer.addChild(ForumlaTitle1);


    CircleSquareExplationText2 = new TextField();
    CircleSquareExplationText2.htmlText = "<font size ='16'>If the circle next to one of the sides you just need to do a square vs square collision check.</font>";
    CircleSquareExplationText2.x = 1046;CircleSquareExplationText2.y = 5;
    CircleSquareExplationText2.width = 200; CircleSquareExplationText2.height = 90;
    CircleSquareExplationText2.wordWrap = true;
    CircleSquareExplationText2.border = true;
    _SpriteLayer.addChild(CircleSquareExplationText2);

    ForumlaText2 = new TextField();
    ForumlaText2.width = 400; ForumlaText2.height = 145;
    ForumlaText2.htmlText = "<font size ='16'>if(Math.abs(line1.vx) &lt; _c1.radius + _s1.width * 0.5)
      {
        if(Math.abs(line1.vy) &lt; _c1.radius + _s1.height * 0.5)
        {
        collision = true;
        }
      }</font>";
    ForumlaText2.x = 1100;ForumlaText2.y = 105;

    ForumlaText2.wordWrap = true;
    ForumlaText2.border = true;
    _SpriteLayer.addChild(ForumlaText2);

    ForumlaTitle2 = new TextField();
    ForumlaTitle2.width = 340; ForumlaTitle2.height = 25;
    ForumlaTitle2.htmlText = "<font size ='16'>Square Collision (notice the corners collide early)</font>";
    ForumlaTitle2.x = 1182;ForumlaTitle2.y = 260;

    ForumlaTitle2.wordWrap = true;
    //ForumlaTitle2.border = true;
    _SpriteLayer.addChild(ForumlaTitle2);

    CircleSquareExplationText3 = new TextField();
    CircleSquareExplationText3.htmlText = "<font size ='16'>However if the circle is next to an edge of the square then you need to use circle vs circle collision check against the edge.</font>";
    CircleSquareExplationText3.x = 1694;CircleSquareExplationText3.y = 5;
    CircleSquareExplationText3.width = 300; CircleSquareExplationText3.height = 80;
    CircleSquareExplationText3.wordWrap = true;
    CircleSquareExplationText3.border = true;
    _SpriteLayer.addChild(CircleSquareExplationText3);

    ForumlaText3 = new TextField();
    ForumlaText3.width = 340; ForumlaText3.height = 260;
    ForumlaText3.htmlText = "<font size ='16'>    if(region == \"topLeft\")
    { CurrentCornerLine =  topLeftLine    }
    else if(region == \"topRight\")
    {CurrentCornerLine =  topRightLine}
    else if(region == \"bottomLeft\")
    {CurrentCornerLine =  bottomLeftLine   }
    else if(region == \"bottomRight\")
    {  CurrentCornerLine =  bottomRightLine   }

    if(CurrentCornerLine .m &lt; _c1.radius)
    {  collision = true; }</font>";
    ForumlaText3.x = 1582;ForumlaText3.y = 100;

    ForumlaText3.wordWrap = true;
    ForumlaText3.border = true;
    _SpriteLayer.addChild(ForumlaText3);

    ForumlaTitle4 = new TextField();
    ForumlaTitle4.width = 340; ForumlaTitle4.height = 50;
    ForumlaTitle4.htmlText = "<font size ='16'>Putting it all together\nClick and drag to view full text</font>";
    ForumlaTitle4.x = 2150;ForumlaTitle4.y = 0;

    ForumlaTitle4.wordWrap = true;
    //ForumlaTitle3.border = true;
    _SpriteLayer.addChild(ForumlaTitle4);

    ForumlaText4 = new TextField();
    ForumlaText4.width = 340; ForumlaText4.height = 400;
    ForumlaText4.htmlText = "<font size ='16'>
<font size ='16'>var region:String = \"\";
    if(_c1.yPos &lt; _s1.yPos)
    {
      if(_c1.xPos &lt; _s1.xPos)
      {	region = \"topLeft\";  }
      else if (_c1.xPos &gt; _s1.xPos + _s1.width)
      {	region = \"topRight\";    }
      else
      {	region = \"topMiddle\";  }
    }
    else if (_c1.yPos &gt; _s1.yPos + _s1.height)
    {
      if(_c1.xPos &lt; _s1.xPos)
      {	region = \"bottomLeft\";     }
      else if (_c1.xPos &gt; _s1.xPos + _s1.width )
      {	region = \"bottomRight\";      }
      else
      {	region = \"bottomMiddle\";    }
    }
    else
    {
      if(_c1.xPos &lt; _s1.xPos )
      {	region = \"leftMiddle\";    }
      else
      {	region = \"rightMiddle\";    }
    }
      if(region == \"topMiddle\"
      || region == \"bottomMiddle\"
      || region == \"leftMiddle\"
      || region == \"rightMiddle\")
      {
        if(Math.abs(line1.vx) &lt; _c1.radius + _s1.width * 0.5)
        {
         if(Math.abs(line1.vy) &lt; _c1.radius + _s1.height * 0.5)
         {
         collision = true;
         }
      }
    else
    {
     if(region == \"topLeft\")
       { CurrentCornerLine =  topLeftLine    }
     else if(region == \"topRight\")
       {CurrentCornerLine =  topRightLine}
     else if(region == \"bottomLeft\")
       {CurrentCornerLine =  bottomLeftLine   }
     else if(region == \"bottomRight\")
       {  CurrentCornerLine =  bottomRightLine   }

     if(CurrentCornerLine .m &lt; _c1.radius)
     {  collision = true; }
    }</font>";
    ForumlaText4.x = 2118;ForumlaText4.y = 40;

    ForumlaText4.wordWrap = true;
    ForumlaText4.border = true;
    _SpriteLayer.addChild(ForumlaText4);

    CircleSizeText = new TextField();
    CircleSizeText.width = 150; CircleSizeText.height = 180;
    CircleSizeText.htmlText = "<font size ='16'></font>";
    CircleSizeText.x = 1200;CircleSizeText.y = 50;

    CircleSizeText.wordWrap = true;
    CircleSizeText.border = true;
    //_SpriteLayer.addChild(CircleSizeText);
    }
public override function Load(inVolume:Bool,inSound:Bool,inKongVar:CKongregate,inDifficulty:Int)
{
 super.Load(inVolume,inSound,inKongVar,inDifficulty);
 buildMap(_platformMap);
 buildMap(_gameObjectMap);
 _CircleModel = new CircleModel("main");
_CircleModel.setX = 64*3.5;_CircleModel.setY = 384;
  _CircleController = new CircleController(_CircleModel);
 _CircleView = new PlayerCircleView(_CircleModel,_CircleController,stage);

  _SpriteLayer.addChild(_CircleView);

  _s1 = new SquareModel();
  _s1.width = 60;_s1.height = 60;
  _s1.xPos = 376;_s1.yPos = 284;
  _s1_View = new SquareView(_s1);
  _SpriteLayer.addChild(_s1_View);


  _s2 = new SquareModel();
  _s2.width = 60;_s2.height = 60;
  _s2.setX = 1272;_s2.setY = 284;
  _s2_View = new SquareView(_s2);
  _SpriteLayer.addChild(_s2_View);

  _s3 = new SquareModel();
  _s3.width = 60;_s3.height = 60;
  _s3.setX = 1968;_s3.setY = 284;
  _s3_View = new SquareView(_s3);
  _SpriteLayer.addChild(_s3_View);


  _VectorModel = new VectorModel(_CircleModel.xPos,_CircleModel.yPos, _s1.xPos+_s1.width/2,_s1.yPos+_s1.height/2);
  _VectorView = new VectorView(_VectorModel,new Rectangle(0,0,0,0),1);
  _SpriteLayer.addChild(_VectorView);
  _VectorTextView = new VectorTextView(_VectorModel,new Rectangle(0,0,0,0));
  _VectorTextView.Text.text = ("line1");
  _SpriteLayer.addChild(_VectorTextView);

  _cornerVectorModel = new VectorModel(0,0,0,0);
  _cornerVectorView = new VectorView(_cornerVectorModel,new Rectangle(0,0,0,0),1);
  _SpriteLayer.addChild(_cornerVectorView);
  _cornerVectorTextView = new VectorTextView(_cornerVectorModel,new Rectangle(0,0,0,0));
  _cornerVectorTextView.Text.text = ("");
  _SpriteLayer.addChild(_cornerVectorTextView);
  
}

public override function checkWin():Bool
{
  if(!_CircleModel.exit)
    {return false;}
  var Door1 = new SquareModel();
  Door1.xPos = 1*64;
  Door1.yPos = 6*64;
  Door1.width = 64;
  Door1.height = 64;
  var boxSquare = new SquareModel();
  boxSquare.xPos = _CircleModel.xPos-_CircleModel.width/2;
  boxSquare.yPos = _CircleModel.yPos-_CircleModel.height/2;
  boxSquare.width = _CircleModel.width;
  boxSquare.height = _CircleModel.height;
  if(_collisionController.squareModelCollision(Door1,boxSquare))
  {
    return true;
  }
    var Door2 = new SquareModel();
  Door2.xPos = 32*64;
  Door2.yPos = 6*64;
  Door2.width = 64;
  Door2.height = 64;
  if(_collisionController.squareModelCollision(Door2,boxSquare))
  {
    return true;
  }
  return  false;
}

  public  override function UnLoad()
  {
  _SpriteLayer.removeChild(_CircleView);
  _SpriteLayer.removeChild(_s1_View);
  _SpriteLayer.removeChild(_s2_View);
  _SpriteLayer.removeChild(_s3_View);
  _SpriteLayer.removeChild(_VectorView);
  _SpriteLayer.removeChild(_VectorTextView);
  _SpriteLayer.removeChild(_cornerVectorView);
  _SpriteLayer.removeChild(_cornerVectorTextView);

      super.UnLoad();
      //_MainCharacterController = null;

  }
public function checkCollisionCircleSquare(_c1:CircleModel,_s1:SquareModel,view:SquareView)
{
  var region = DetermineSpot(_c1,_s1);
  var collision = false;
      if(region == "topMiddle"
      || region == "bottomMiddle"
      || region == "leftMiddle"
      || region == "rightMiddle")
      {
	collision = SquareCollisionPart(_c1,_s1);
      }
      else
      {
	var cornerV0 = GetCornerCollisionPart(_c1,_s1,region);
	if(cornerV0.m < _c1.radius)
	{
	  collision = true;
	}
      }
  if(collision)
  {
    view.hit();
  }
  else
  {
    view.unHit();
  }
}
public function SquareCollisionPart(_c1:CircleModel,_s1:SquareModel):Bool
{
      var _v0:VectorModel = new VectorModel(_c1.xPos,_c1.yPos, _s1.xPos+_s1.width/2,_s1.yPos+_s1.height/2);
      var collision = false;
      if(Math.abs(_v0.vx) < _c1.radius + _s1.width * 0.5)
      {
	if(Math.abs(_v0.vy) < _c1.radius + _s1.height * 0.5)
	{
	  collision = true;
	}
      }
      return collision;
}
public function GetCornerCollisionPart(_c1:CircleModel,_s1:SquareModel,region:String):VectorModel
{
    var cornerV0 :VectorModel = new VectorModel();
    if(region == "topLeft")
    {
     cornerV0 = new VectorModel
	(
	_c1.xPos,
	_c1.yPos,
	_s1.xPos,
	_s1.yPos
	);
    }
    else if(region == "topRight")
    {
      cornerV0 = new VectorModel
	(
	_c1.xPos,
	_c1.yPos,
	_s1.xPos + _s1.width,
	_s1.yPos
	);

    }
    else if(region == "bottomLeft")
    {
      cornerV0 = new VectorModel
	(
	_c1.xPos,
	_c1.yPos,
	_s1.xPos,
	_s1.yPos +_s1.height
	);
    }
    else if(region == "bottomRight")
    {
      cornerV0 = new VectorModel
	(
	_c1.xPos,
	_c1.yPos,
	_s1.xPos +_s1.width,
	_s1.yPos +_s1.height
	);
    }
    return cornerV0;
}
public function DetermineSpot(_c1:CircleModel,_s1:SquareModel):String
{
    var region:String = "";
    //Is the circle above the rectangle's top edge?
    if(_c1.yPos < _s1.yPos)
    {
      //If it is, we need to check whether it's in the 
      //top left, top center or top right
      if(_c1.xPos < _s1.xPos)
      {
	region = "topLeft";
      }
      else if (_c1.xPos > _s1.xPos + _s1.width)
      {
	region = "topRight";
      }
      else
      {
	region = "topMiddle";
      }
    }
    //below the bottom edge?
    else if (_c1.yPos > _s1.yPos + _s1.height)
    {
      //If it is, we need to check whether it's in the 
      //bottom left, bottom center or bottom right
      if(_c1.xPos < _s1.xPos)
      {
	region = "bottomLeft";
      }
      else if (_c1.xPos > _s1.xPos + _s1.width )
      {
	region = "bottomRight";
      }
      else
      {
	region = "bottomMiddle";
      }
    }
    // the left or right side?
    else
    {
      if(_c1.xPos < _s1.xPos )
      {
	region = "leftMiddle";
      }
      else
      {
	region = "rightMiddle";
      }
    }
    return region;
}
override function OnEnter(e:flash.events.Event)
{
    try
    {
      if(checkWin())
      {	
	_CircleModel.exit = false;
	UnLoad();
	this.removeEventListener(Event.ENTER_FRAME, OnEnter);
	dispatchEvent(new Event("LevelFinished"));return;
      }
      _CircleModel.exit = false;
      if(Pause == "Paused")
      {
	return;
      }	

      _cornerVectorModel.update(0,0,0,0);
      _VectorModel.update(0,0,0,0);
      if(_CircleModel.xPos < 832)
      {
	RegionText.htmlText = "<font size='16'>Region:  "+ DetermineSpot(_CircleModel,_s1) +"</font>";
	
      }
      else if(_CircleModel.xPos < 1600)
      {
	_VectorModel.update(_CircleModel.xPos,_CircleModel.yPos, _s2.xPos+_s1.width/2,_s2.yPos+_s2.height/2);
	if(SquareCollisionPart(_CircleModel,_s2))
	{
	  _s2_View.hit();
	}
	else
	{
	  _s2_View.unHit();
	}
      }
      else
      {	
	var Region = DetermineSpot(_CircleModel,_s3);
	var cornerV0 = GetCornerCollisionPart(_CircleModel,_s3,Region);
	_cornerVectorModel.update(cornerV0.a.x,cornerV0.a.y,cornerV0.b.x,cornerV0.b.y);
	_cornerVectorTextView.Text.text = Region + "Line";
	checkCollisionCircleSquare(_CircleModel,_s3,_s3_View);
      }
	
      
      //Clear the stage bitmap from the previous frame so that it's
      //blank when you add the new tile positions
      _foregroundBitmapData.fillRect(_foregroundBitmapData.rect, 0);
      //Set up the camera to focus on the cat
      _camera.x = _CircleModel.xPos-Constants.flashWidth*0.5;
      _camera.y = 0;
      
      //Check the camera's game world boundaries
      //Left
      if(_camera.x < 0)
      {
	_camera.x = 0;
      }
      
      //Right
      if(_camera.x > (MAP_COLUMNS * MAX_TILE_SIZE) 
	- Constants.flashWidth)
      {
	_camera.x = (MAP_COLUMNS * MAX_TILE_SIZE) - Constants.flashWidth;
      }
      
      //Bottom
      if(_camera.y > (MAP_ROWS * MAX_TILE_SIZE) - Constants.flashHeight)
      {
	_camera.y = (MAP_ROWS * MAX_TILE_SIZE) - Constants.flashHeight;
      }
      
      //Top
      if(_camera.y < 0)
      {
	_camera.y = 0;
      }
    
      //Update the box's Model
      _CircleModel.update();
      //if Shoting
//       shotVector.update(0, 0,0,0);
//       	var y2 =  _CircleModel.yPos + _CircleModel.height/2;
// 	var y1 = mouseY;
// 	
// 	var x2 = _CircleModel.xPos +_CircleModel.width/2;
// 	var x1 = mouseX+_camera.x;
// 	var m = (y2 - y1 )/(x2 - x1);
// 	var b = (y2) - m*(x2);
// 	var testVector = new VectorModel();
// 	testVector.update(x2, y2,x1,y1);

    _collisionMap = [[]];
    for(row in 0...MAP_ROWS)
    {
      _collisionMap[row] = [];
      for(column in 0...MAP_ROWS)	
      {
	_collisionMap[row][column] = new List<TileModel>();
      }
    }
    for(enemy in _enemies)
    {
	_collisionMap[enemy.model.mapRow][enemy.model.mapColumn].add(enemy.model);
	enemy.model.update();
      if (enemy.model.xPos + (enemy.model.width) > maxX)
      {
	enemy.model.setX = maxX - (enemy.model.width);
	enemy.model.vx = 0;
      }
      else if (enemy.model.xPos < minX)
      {
	      enemy.model.setX = minX;
	      enemy.model.vx = 0;
      }
      if (enemy.model.yPos < 0)
      {
	      enemy.model.setY = 0;
	      enemy.model.vy = 0;
      }

      else if (enemy.model.yPos + enemy.model.height > Constants.flashHeight && enemy.model.vy > 0)
      {
	      enemy.model.setY = Constants.flashHeight - enemy.model.height;
	      enemy.model.vy = 0;
      } 
	drawGameObject(enemy.model,_foregroundBitmapData);
    }
// 	for(column in -1...2)
// 	{
// 	  for(row in -1...2)
// 	  {
// 	    if(_CircleModel.mapRow + row < _collisionMap.length &&
// 	       _CircleModel.mapRow + row >= 0 &&
// 	       _CircleModel.mapColumn + column < _collisionMap[0].length &&
// 	       _CircleModel.mapColumn + column >= 0)
// 	    {		
// 	      	for(model in _collisionMap[_CircleModel.mapRow + row ][_CircleModel.mapColumn + column])
// 		{
// 		  //collision
// 		}
// 	    }
// 	  }
// 	}
      //Stop the box at the stage boundaries
      //StageBoundaries.stopBitmap(_CircleModel, stage);
      maxX = MAP_COLUMNS * MAX_TILE_SIZE;
      if (_CircleModel.xPos + (_CircleModel.width/2) > maxX)
      {
	_CircleModel.setX = maxX - (_CircleModel.width/2);
	_CircleModel.vx = 0;
      }
      else if (_CircleModel.xPos-(_CircleModel.width/2) < minX)
      {
	      _CircleModel.setX = minX+(_CircleModel.width/2);
	      _CircleModel.vx = 0;
      }
      if (_CircleModel.yPos - _CircleModel.height/2 < 0)
      {
	      _CircleModel.setY =  _CircleModel.height/2;
	      _CircleModel.vy = 0;
      }
      else if (_CircleModel.yPos + _CircleModel.height/2 > Constants.flashHeight && _CircleModel.vy > 0)
      {
	      _CircleModel.setY = Constants.flashHeight - _CircleModel.height/2;
	      _CircleModel.vy = 0;
      } 
      
 
      //Check for collisions with the platforms
//       _collisionController.platformCollision
// 	(_CircleModel, _platformMap, MAX_TILE_SIZE, PLATFORM);

       _collisionController.platformCollisionCircleModel(_CircleModel,_platformMap,MAX_TILE_SIZE,PLATFORM);

      //Scroll the game world
      _foregroundBitmap.scrollRect = _camera;
      _backgroundBitmap.scrollRect = _camera;
      _SpriteLayer.scrollRect = _camera;

      //_gunView.scrollRect = _camera;
      //drawGunObject(_gunModel,_currentGunBitmapData, _foregroundBitmapData); 
    }
catch(err:Dynamic)
    {
    trace(err.message);
    }
}

  public override function buildReserveEnemies()
  {
  }
}